﻿/*
--------------------------------------
File: SaltAndPepperNoise.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
Adds random salt and pepper noise to the image
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
    class SaltAndPepperNoise : VisionAlgorithm
    {
        public SaltAndPepperNoise(String name) : base(name) {
            hasConfiguration = true;
            configuration = new SaltAndPepperNoiseConfiguration();
        }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Bitmap orignalImage = new Bitmap(sourceImage);
            Bitmap returnImage = new Bitmap(sourceImage);
            
            int[] rgbValues = MyImage.BmpToArray(returnImage);
            //The Amount is the global amount of noise in percentage
            int Amount = _configValue;
            Random TempRandom = new Random();

            //Modified pixelArray            
            int[] rgbValues2 = rgbValues;

            int width = sourceImage.Width;
            int height = sourceImage.Height;

            //Loops through all the pixels in the array
            for (int i = 0; i < width * height; i++)
            {
                if (TempRandom.Next(0, 101) < Amount)
                {
                    //Adds random white or black noise
                    if (TempRandom.Next(0, 2) == 1)
                    {
                        rgbValues2[i] = -16777216;
                    }
                    else
                    {
                        rgbValues2[i] = -1;
                    }
                }
            }          
            //Returns the modified image
            returnImage = MyImage.ArrayToBmp(rgbValues2, returnImage);
            FilterTest.NoiseTest(orignalImage, returnImage);
            return returnImage;
        }
    }
}


